using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CursorManagers : MonoBehaviour
{
    public Texture2D cursorTextureSolid; // 实心光标纹理
    public Texture2D cursorTextureDashed; // 虚线光标纹理
    public GameObject prefabToPlace; // 要放置的 Prefab
    public LayerMask targetLayer; // 目标层
    public HostDate host; // 主机对象，用于获取资产数量

    private bool isCursorModeActive = false;

    public void StartCursorMode()
    {
        if (!isCursorModeActive)
        {
            isCursorModeActive = true;
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
            Cursor.SetCursor(cursorTextureDashed, new Vector2(cursorTextureDashed.width / 2, cursorTextureDashed.height / 2), CursorMode.Auto);
        }
    }

    void Update()
    {
        if (isCursorModeActive)
        {
            // 获取鼠标位置
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            
            if (Physics.Raycast(ray, out hit, 100f, targetLayer))
            {
                Debug.Log("Hit: " + hit.collider.gameObject.name + " with tag: " + hit.collider.tag);
                if (hit.collider.CompareTag("Road"))
                {
                    // 如果碰撞体标签为 "Road"，显示实心光标
                    Cursor.SetCursor(cursorTextureSolid, new Vector2(cursorTextureSolid.width / 2, cursorTextureSolid.height / 2), CursorMode.Auto);

                    if (Input.GetMouseButtonDown(0) && host != null && host.assets > 60)
                    {
                        // 如果资产数量大于60，放置 Prefab
                        Instantiate(prefabToPlace, hit.point, Quaternion.identity);
                        host.assets -= 60; // 假设放置Prefab需要消耗一定资产
                        EndCursorMode();
                    }
                }
                else
                {
                    // 其他标签，显示虚线光标
                    Cursor.SetCursor(cursorTextureDashed, new Vector2(cursorTextureDashed.width / 2, cursorTextureDashed.height / 2), CursorMode.Auto);
                }
            }
            else
            {
                // 没有碰撞到任何物体，显示虚线光标
                Cursor.SetCursor(cursorTextureDashed, new Vector2(cursorTextureDashed.width / 2, cursorTextureDashed.height / 2), CursorMode.Auto);
            }
        }
    }

    private void EndCursorMode()
    {
        isCursorModeActive = false;
        Cursor.visible = true;
        Cursor.lockState = CursorLockMode.None;
        Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto); // 恢复默认光标
    }
}
